﻿using AutoMapper;
using FoodModel;
using J.FoodManageSet.Application.Command.SpecificationCommand;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageSet.Application.CommandHandler.SpecificationCommandHandler
{
    /// <summary>
    /// 菜品规格添加或修改
    /// </summary>
    public class SpecificationAddCommandHandler : IRequestHandler<SpecificationAddCommand, int>
    {
        private readonly ISpecificationRepository _specificationRepository;
        private readonly IMapper _mapper;

        public SpecificationAddCommandHandler(ISpecificationRepository specificationRepository,IMapper mapper)
        {
            _specificationRepository = specificationRepository;
           _mapper = mapper;
        }

        public async Task<int> Handle(SpecificationAddCommand request, CancellationToken cancellationToken)
        {
            int count = 0;
            if(request.SpecificationId==0)
            {
                SpecificationModel model = _mapper.Map<SpecificationModel>(request);
                model.Separates = model.Separates;
                count =await _specificationRepository.SpecificationAdd(model);
            }
            else
            {
                SpecificationModel model = _mapper.Map<SpecificationModel>(request);
                 model.Separates = model.Separates;
                count = await _specificationRepository.SpecificationUpDate(model);
            }
            return count;
        }
    }
}
